
package com.be.beadmin.product.impl;

import com.be.beadmin.common.utils.JpaQueryBuilderNew;
import com.be.beadmin.product.dto.input.ErrorReportFileInfoPage;
import com.be.beadmin.product.entity.ErrorReport;
import com.be.beadmin.product.entity.ErrorReportFileInfo;
import com.be.beadmin.product.entity.ErrorReportFileInfo_;
import cn.hutool.core.util.ObjectUtil;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaSortBuilder;
import com.be.beadmin.project.Util.MinioUtil;
import com.be.beadmin.service.impl.MinioService;
import com.be.beadmin.service.repository.DictTypeRepository;
import com.be.beadmin.service.service.DictDataService;
import com.be.beadmin.service.service.SerialNumberService;
import lombok.RequiredArgsConstructor;
import com.be.beadmin.product.mapper.ErrorReportFileInfoMapperEx;
import com.be.beadmin.product.repository.ErrorReportFileInfoRepository;
import com.be.beadmin.product.service.ErrorReportFileInfoService;
import com.be.beadmin.product.dto.input.ErrorReportFileInfoDto;
import com.be.beadmin.product.dto.output.ErrorReportFileInfoEx;
import com.be.beadmin.product.dto.input.ErrorReportFileInfoQueryCriteria;
import com.be.beadmin.product.mapper.ErrorReportFileInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Order;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
* @description 服务实现
* @author ljh
* @date 2023-09-21
**/
@Service
@RequiredArgsConstructor
public class ErrorReportFileInfoServiceImpl implements ErrorReportFileInfoService {

    @Autowired
    private  ErrorReportFileInfoRepository errorReportFileInfoRepository;
    @Autowired
    private  ErrorReportFileInfoMapper errorReportFileInfoMapper;
    @Autowired
    private  ErrorReportFileInfoMapperEx errorReportFileInfoExMapper;


    @Autowired
    private SerialNumberService serialNumberService;

    @Autowired
    private MinioService minioService;


    MinioUtil minioUtil = new MinioUtil();

    @Override
    public Page<ErrorReportFileInfoEx> getErrorReportFileInfoByPage(PageEntity pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<ErrorReportFileInfo> page = errorReportFileInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilderNew.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(ErrorReportFileInfo_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(ErrorReportFileInfo_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<ErrorReportFileInfoEx> page1 = page.map(errorReportFileInfoExMapper::toEntity);
            return page1;
    }

    @Override
    public Page<ErrorReportFileInfoEx> getErrorReportFileInfoPageByErrorReportId(ErrorReportFileInfoPage errorReportFileInfoPage) {
        PageEntity pageEntity = errorReportFileInfoPage.getPageEntity();
        String errorReportId = errorReportFileInfoPage.getErrorReportId();
        Pageable pageable = PageRequest.of(
                pageEntity.getCurrent() -1,
                pageEntity.getPageSize());
        Page<ErrorReportFileInfo> page = errorReportFileInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilderNew.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(ErrorReportFileInfo_.errorReportId), errorReportId); //过滤为0
                    list.add(pre2);
                    Predicate pre3 = criteriaBuilder.equal(root.get(ErrorReportFileInfo_.delete), 0); //过滤为0
                    list.add(pre3);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(ErrorReportFileInfo_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                }
                , pageable);
        Page<ErrorReportFileInfoEx> page1 = page.map(errorReportFileInfoExMapper::toEntity);
        return page1;
    }

    @Override
    public String getErrorPreviewUrl(String errorReportFileInfoId) throws Exception {
        ErrorReportFileInfo errorReportFileInfo = errorReportFileInfoRepository.findErrorReportFileInfoByIdAndDelete(errorReportFileInfoId,0);
        String fileCode = errorReportFileInfo.getFileCode();
        String fileName = errorReportFileInfo.getFileName();
        String FileName = fileCode+"+"+fileName ;
        String preview_url = minioUtil.getPreviewFileUrl(errorReportFileInfo.getBucketName(),FileName);
        errorReportFileInfo.setAttachmentUrl(preview_url);
        errorReportFileInfoRepository.save(errorReportFileInfo);
        return preview_url;
    }

    @Override
    public void uploadErrorReportFileInfo(String errorReportId, MultipartFile[] multipartFiles, String bucketName) throws Exception {
        for(MultipartFile multipartFile:multipartFiles){
            //附件相关
            if(multipartFile!=null) {

                ErrorReportFileInfo errorReportFileInfo =new ErrorReportFileInfo();

                errorReportFileInfo.setFileCode(serialNumberService.getNextSnByCode("FileCode"));
                errorReportFileInfo.setFileName( multipartFile.getOriginalFilename());
                errorReportFileInfo.setBucketName(bucketName);
                String uploadFileName = errorReportFileInfo.getFileCode()+"+"+errorReportFileInfo.getFileName();
                String preview_url = minioUtil.uploadFile2(multipartFile,uploadFileName,bucketName);
                errorReportFileInfo.setAttachmentUrl(preview_url);

                errorReportFileInfo.setErrorReportId(errorReportId);

                errorReportFileInfoRepository.save(errorReportFileInfo);
            }
        }

    }

    @Override
    public void addErrorReportFileInfo(ErrorReportFileInfoDto errorReportFileInfoDto) {
            ErrorReportFileInfo result = errorReportFileInfoRepository.findErrorReportFileInfoByIdAndDelete(errorReportFileInfoDto.getId(), 0);
            if(ObjectUtil.isNotEmpty(result)) {
                throw new BeadminException("ErrorReportFileInfo不存在,请重新查询");
            }
            ErrorReportFileInfo errorReportFileInfo = errorReportFileInfoMapper.toEntity(errorReportFileInfoDto);
            errorReportFileInfoRepository.save(errorReportFileInfo);
    }

    @Override
    public void updateErrorReportFileInfo(ErrorReportFileInfoDto errorReportFileInfoDto) {
            ErrorReportFileInfo errorReportFileInfo = errorReportFileInfoRepository.findErrorReportFileInfoById(errorReportFileInfoDto.getId());
            errorReportFileInfo = errorReportFileInfoMapper.toEntity(errorReportFileInfoDto);
            errorReportFileInfoRepository.save(errorReportFileInfo);
    }


    @Override
    public void delErrorReportFileInfo(String id) {
        ErrorReportFileInfo errorReportFileInfo = errorReportFileInfoRepository.findErrorReportFileInfoById(id);
        errorReportFileInfo.setDelete(1);
        errorReportFileInfoRepository.save(errorReportFileInfo);
    }

    @Override
    public void delErrorReportFileInfos (List<String> ids) { }


}